package com.yes_iot.controller;

import com.yes_iot.entity.VisualMoudle;
import com.yes_iot.entity.VisualMoudleLibrary;
import com.yes_iot.util.FileUtil;
import com.yes_iot.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/yesiot")
@Slf4j
@CrossOrigin
public class VisualMoudleController extends BaseController{
    @Value("${file.oss.basePath}")
    private String basePath;
    @RequestMapping(value = "/moudle/visual", method = RequestMethod.GET)
    public Result selectPage(@RequestParam String parentId) {
        try {
            List<VisualMoudle> visualMoudleList= visualMoudleService.findVisualMoudelByParentId(parentId);

            return Result.succeed(visualMoudleList,"查询成功");
        } catch (Exception e) {
            e.getMessage();
            return Result.failed(e.getMessage());
        }
    }

    @RequestMapping(value = "/moudle/visual/save", method = RequestMethod.POST)
    public Result save(@RequestBody VisualMoudle visualMoudle) {
        try {
            UUID id = visualMoudle.getId();
            if (id==null){
                visualMoudle.setId(UUID.randomUUID());
                visualMoudle.setCreatedTime(System.currentTimeMillis());
            }
            return Result.succeed(visualMoudleService.save(visualMoudle),"查询成功");
        } catch (Exception e) {
            e.getMessage();
            return Result.failed(e.getMessage());
        }
    }

    @RequestMapping(value = "/moudle/visual", method = RequestMethod.DELETE)
    public Result delete(@RequestParam String id) {
        try {
            VisualMoudle visualMoudle= visualMoudleService.findById(id);
            if (visualMoudle!=null){
                visualMoudleService.deleteById(id);
            }
            return Result.succeed("删除成功");
        } catch (Exception e) {
            e.getMessage();
            return Result.failed(e.getMessage());
        }
    }
}
